﻿(function () {
    var Tree = function (pContainer) {
        var _container = pContainer,
                fold = function (pDom) {
                    if (pDom.innerHTML === "+") {
                        pDom.innerHTML = "-";
                        pDom.parentNode.parentNode.getElementsByTagName("UL")[0].style.display = "";
                    } else {
                        pDom.innerHTML = "+";
                        pDom.parentNode.parentNode.getElementsByTagName("UL")[0].style.display = "none";
                    }
                },
                selectAll = function (pDom) {
                    if (pDom.getAttribute("data-all") != null) {
                        var _root = pDom.parentNode.parentNode.parentNode,
                            _allInput = _root.getElementsByTagName("INPUT"),
                            _check = pDom.checked;
                        for (var i = 0, l = _allInput.length; i < l; i++) {
                            _allInput[i].checked = _check;
                        }
                    }
                },
                init = function () {
                    $(_container).bind("click", function (event) {
                        var me = this,
                            _target = event.target;
                        switch (_target.tagName) {
                            case "A": fold.call(me, _target); break;
                            case "INPUT": selectAll.call(me, _target); break;
                        }
                    });
                };
        init();
    },
    Merger = function (pContainer, checkboxContainer) {
        var _container = pContainer,
            _btnMerger = _container.getElementsByTagName("INPUT")[0],
            _selectorZip = _container.getElementsByTagName("SELECT")[0],
            _url = "MergerHandleSingle.ashx",
            _checkboxs = checkboxContainer.getElementsByTagName("INPUT"),
            _rootPath = checkboxContainer.getAttribute("data-path"),
            _loadingPanel = $("#LoadingPanel"),
            _loadingTip = _loadingPanel.find("STRONG"),
            _zipArray = [],
            _nameArray = [],
            _resultPanel = $("#result"),
            _resultH3 = _resultPanel.find("H3")[0],
            _resultErrContainer = _resultPanel.find("UL")[0],
            _errList = [],
            _done = 0,
            _txtMerger = $("#txtMerger"),
            merger = function () {
                _loadingPanel.mask();
                $.ajax({
                    url: _url,
                    scriptCharset: "utf-8",
                    dataType: "json",
                    type: "post",
                    data: {
                        "filePath": _zipArray.join(","),
                        "fileName": _nameArray.join("_"),
                        "rootPath": _rootPath,
                        "mode": _selectorZip.value
                    },
                    success: function (data) {
                        if (data.type) {
                            _txtMerger.val(data.fileName);
                        } else {
                            _txtMerger.val(data.err);
                        }
                        _loadingPanel.unmask();
                    }
                });
            },
            showResult = function () {

            },
            getArray = function () {
                _zipArray = [];
                _nameArray = [];
                for (var i = 0, l = _checkboxs.length; i < l; i++) {
                    if (_checkboxs[i].getAttribute("data-all") == null && _checkboxs[i].value != null && _checkboxs[i].checked) {
                        _zipArray.push(_checkboxs[i].value);
                        _nameArray.push(_checkboxs[i].getAttribute("data-name"));
                    }
                }
            },
            init = function () {
                var me = this;
                $(_btnMerger).bind("click", function () {
                    getArray.call(me);
                    if (_zipArray.length === 0) {
                        alert("同学，你还没有选择要合并的文件！");
                    } else {
                        merger.call(me);
                    }
                })
            };
        init();
    };
    $(document).ready(function () {
        var _treeContainer = document.getElementById("rootContainer"),
            _submitContainer = document.getElementById("submitContainer");
        new Tree(_treeContainer);
        new Merger(_submitContainer, _treeContainer);
    });
})();